//引用express框架
const express = require('express');
//引用route路由
const getCompleteInfor = express.Router();
//引用数据库操作逻辑
var TaskJoin = require('../model/TaskJoin')
var UserBind = require('../model/userBind')
var User = require('../model/user')
// 返回前端信息

getCompleteInfor.get('/', (req, res) => {
    let data = {
        status: 200,
        tips: 'success',
        data: ''
    }
    let respon = res
    // 获取当前请求页
    let id = req.query.taskId

    const test = async () => {
        // 查询赏金任务
        TaskJoin.find({
            'taskId': id,
        }, function (err, doc) {
            // 若失败则告知原因，并返回
            if (err) {
                data.tips = err
                respon.status(200).json(data)
            }
            let temp = doc
            for (let i = 0; i < doc.length; i++) {
                UserBind.find({
                    'openId': doc[i].openId,
                }, function (err, doc) {
                    User.find({
                        '_id': doc[0].userId,
                    }, function (err, doc) {
                        let t = {
                            data: temp[i],
                            user: doc[0]
                        }
                        temp[i] = t
                        if (i == doc.length - 1) {
                            data.data = temp
                            respon.status(200).json(data)
                        }
                    })
                })
            }
        })
    }
    test()
})
//将路由对象做为模块成员进行导出
module.exports = getCompleteInfor;